# coding=utf-8

from PySide6.QtCore import (Slot, QSettings)
from PySide6.QtWidgets import (QMenu, QMainWindow, QApplication, QMessageBox, QStyle, QSystemTrayIcon)
from PySide6.QtGui import (QAction, QCloseEvent, QIcon)

from laadsOrderTool.ui.ui_pyLOT import Ui_MainWindow
from laadsOrderTool.Settings import Settings
from laadsOrderTool.InputOrderUrl import InputOrderUrl

from laadsOrderTool.i1n import translations_rc
from laadsOrderTool.icons import icons_rc


def check_auth() -> bool:
    local_settings = QSettings('laadsOrderTool', 'laadsOrderTool')
    return local_settings.value('username') and local_settings.value('password')


class PyLOT(QMainWindow):
    """主界面类"""

    def __init__(self) -> None:
        super(PyLOT, self).__init__()
        # 系统托盘图标相关组件
        self.tray_icon = QSystemTrayIcon(QIcon(':/icon.jpg'))
        self.tray_icon_menu = QMenu()
        self.minimize_action = QAction(self.tr('Minimize'))
        self.maximize_action = QAction(self.tr('Maximize'))
        self.restore_action = QAction(self.tr('Restore'))
        self.quit_action = QAction(self.tr('Quit'))
        # 界面初始化
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.init_face()

        # 界面信号连接
        self.ui.actionAbout_Qt.triggered.connect(QApplication.aboutQt)
        self.ui.actionAbout.triggered.connect(self.on_about)
        self.ui.actionOptions.triggered.connect(self.on_settings)
        self.ui.actionNew_Task.triggered.connect(self.on_new)

    def about_system_tray(self) -> None:
        # 托盘图标

        # 托盘图标右键菜单
        self.tray_icon_menu.addAction(self.minimize_action)
        self.tray_icon_menu.addAction(self.maximize_action)
        self.tray_icon_menu.addAction(self.restore_action)
        self.tray_icon_menu.addSeparator()
        self.tray_icon_menu.addAction(self.quit_action)
        self.tray_icon.setContextMenu(self.tray_icon_menu)
        # 托盘图标右键菜单的信号连接
        self.minimize_action.triggered.connect(self.hide)
        self.maximize_action.triggered.connect(self.showMaximized)
        self.restore_action.triggered.connect(self.showNormal)
        self.quit_action.triggered.connect(self.close)

        self.tray_icon.show()

    def closeEvent(self, event: QCloseEvent) -> None:
        """关闭时隐藏窗口到托盘而不时关闭窗口"""
        self.hide()
        event.accept()

    def init_face(self) -> None:
        """设置一些在ui文件中不能设置的界面组件功能"""
        self.ui.actionAbout.setIcon(
            self.style().standardIcon(QStyle.SP_MessageBoxInformation))
        self.ui.actionAbout_Qt.setIcon(
            self.style().standardIcon(QStyle.SP_TitleBarMenuButton))

        self.about_system_tray()
    @Slot()
    def on_about(self) -> None:
        """生成关于窗口，显示关于信息"""
        msg_box = QMessageBox()
        msg_box.setWindowTitle("LAADS Order Tool")
        msg_box.setText("The document has been modified.")
        msg_box.exec()

    @Slot()
    def on_settings(self) -> None:
        """生成设置窗口，与ui.actionOptions绑定"""
        setting_dialog = Settings()
        setting_dialog.exec()

    @Slot()
    def on_new(self):
        if not check_auth():
            QMessageBox.critical(self, self.tr("Auth Error"),
                                 self.tr('User authentication information is not detected'))
            return
        url_dialog = InputOrderUrl()
        url_dialog.exec()
